/*
 * Created on 15.03.2005
 *
 */
package de.fmannan.addbook.editor.types;

import java.util.Date;


/**
 * @author hmartin
 */
public class TypesUtil
{
    /**
     * Return an invalid int.
     *
     * @return
     */
    public static int getInvalidInt()
    {
        return InvalidTypes.INVALID_INT;
    }


    /**
     * Test for valid int.
     *
     * @param intValue
     * @return
     */
    public static boolean isValidInt(int intValue)
    {
        boolean ret;
        if (intValue == InvalidTypes.INVALID_INT)
        {
            ret = false;
        }
        else
        {
            ret = true;
        }
        return ret;
    }


    /**
     * Return an invalid long.
     *
     * @return
     */
    public static long getInvalidLong()
    {
        return InvalidTypes.INVALID_LONG;
    }


    /**
     * Test for valid long.
     *
     * @param longValue
     * @return
     */
    public static boolean isValidLong(long longValue)
    {
        boolean ret;
        if (longValue == InvalidTypes.INVALID_LONG)
        {
            ret = false;
        }
        else
        {
            ret = true;
        }
        return ret;
    }

    /**
     * Test for valid Long.
     *
     * @param value
     * @return
     */
    public static boolean isValidLong(Long value)
    {
        boolean ret;

        if (value != null)
        {
            if (value.longValue() == InvalidTypes.INVALID_LONG)
            {
                ret = false;
            }
            else
            {
                ret = true;
            }
        }
        else
        {
            ret = false;
        }

        return ret;
    }


    /**
     * Return an invalid double.
     *
     * @return
     */
    public static double getInvalidDouble()
    {
        return InvalidTypes.INVALID_DOUBLE;
    }


    /**
     * Return an invalid empty double.
     *
     * @return
     */
    public static double getInvalidEmptyDouble()
    {
        return InvalidTypes.INVALID_EMPTY_DOUBLE;
    }


    /**
     * Test for valid long.
     *
     * @param doubleValue
     * @return
     */
    public static boolean isValidDouble(double doubleValue)
    {
        boolean ret;
        if (Double.isInfinite(doubleValue) ||  Double.isNaN(doubleValue) || doubleValue == InvalidTypes.INVALID_DOUBLE) //changed adrian 18.04.2005
        {
            ret = false;
        }
        else
        {
            ret = true;
        }
        return ret;
    }

    public static boolean isValidString(String string)
    {
        boolean ret;
        if ( string != null ) {
            if (string.equals(InvalidTypes.INVALID_STRING)) 
            {
                ret = false;
            }
            else
            {
                ret = true;
            }            
        }
        else {
            ret = false;
        }
        return ret;
    }

    /**
     * Test for valid long.
     *
     * @param doubleValue
     * @return
     */
    public static boolean isValidEmptyDouble(double doubleValue)
    {
        boolean ret;
        if  (doubleValue == InvalidTypes.INVALID_EMPTY_DOUBLE)
        {
            ret = true;
        }
        else
        {
            ret = false;
        }
        return ret;
    }


    /**
     * Return an invalid date.
     *
     * @return
     */
    public static Date getInvalidDate()
    {
    	// TODO try to get rid of this - its really useless
        return new Date();
    }




    /**
     * Test for date.
     *
     * @param dateValue
     * @return
     */
    public static boolean isValidDate(Date dateValue)
    {
        boolean ret;
        if (dateValue != null)
        {
            return true;
        }else {
        	return false;
        }
    }


    /**
     * Return an invalid object id.
     *
     * @return
     */
    public static Long getInvalidId()
    {
        return InvalidTypes.INVALID_OBJECT_ID;
    }


    /**
     * Test for valid object id.
     *
     * @param id
     * @return
     */
    public static boolean isValidId(Long id)
    {
        boolean ret;
        if (id != null)
        {
            if (id.equals(InvalidTypes.INVALID_OBJECT_ID))
            {
                ret = false;
            }
            else
            {
                ret = true;
            }
        }
        else
        {
            ret = false;
        }

        return ret;
    }




    /**
     * Return an invalid String.
     *
     * @return
     */
    public static String getInvalidDatabaseString()
    {
        return InvalidTypes.INVALID_STRING;
    }

    /**
     * Return an invalid String.
     *
     * @return
     */
    public static String getInvalidString()
    {
        return InvalidTypes.INVALID_STRING;
    }

    public static double getValidDouble (double system, double user)
    {
        double ret;

        if (isValidDouble(user))
        {
            ret = user;
        }
        else
        {
            ret = system;
        }

        return ret;
    }

    public static int getValidInt (int system, int user)
    {
        int ret;

        if (isValidInt(user))
        {
            ret = user;
        }
        else
        {
            ret = system;
        }

        return ret;
    }

}
